package com.eascs.loan.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.loan.entity.O2oBankLoanData;
import com.eascs.loan.form.O2oLoanForm;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

/**
 * 
 * @author lwx
 *
 */
@Repository
public class O2oLoanDao extends BaseO2oDao {
	/**
	 * 面签申报 保存贷后信息
	 */
	public Map<String, String> saveLoan(O2oLoanForm vo) {
		Map<String, String> map = new HashMap<String, String>();
		String flag = "";
		O2oBankLoanData bank = new O2oBankLoanData();
		bank.setBillCode(vo.getBillCode());
		bank.setApplyAmount(vo.getApplyAmount());
		bank.setLoanBank(vo.getLoanBankCode());
		bank.setLoanAmount(vo.getLoanAmount());
		bank.setLoanRate(vo.getLoanRate());
		bank.setLoanTerm(vo.getLoanTerm());
		bank.setDataType(vo.getDataType());
		bank.setLoanerName(vo.getLoanerName());
		bank.setLoanerCard(vo.getLoanerCard());
		bank.setPayType(vo.getPayType());
		bank.setIsDel("N");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (vo.getLoanDate() != null) {
			try {
				bank.setLoanDate(sdf.parse(vo.getLoanDate()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		boolean insrtFlag = false;
		String keyCode = isBillCodeExport(vo.getBillCode());
		if (StringUtils.isNotEmpty(keyCode)) {// 修改
			O2oBankLoanData o2oBankLoanData=queryBillCode(vo.getBillCode());
			Date date = new Date();
			bank.setUpdateTime(date);
			User user = UserHolder.getUser();
			if (user != null) {
				bank.setUpdatePsn(UserHolder.getUser().getCname());
			}
			bank.setBankLoanId(keyCode);
			bank.setOpratePsn(o2oBankLoanData.getOpratePsn());
			bank.setOprateTime(o2oBankLoanData.getOprateTime());
			try {
				insrtFlag = this.updateById(bank) >= 1 ? true : false;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {// 新增
			Date date = new Date();
			bank.setOprateTime(date);
			User user = UserHolder.getUser();
			if (user != null) {
				bank.setOpratePsn(user.getCname());
				bank.setUpdatePsn(user.getCname());
			}
			bank.setUpdateTime(date);
			try {
				insrtFlag = this.insert(bank) >= 1 ? true : false;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if (insrtFlag == true) {
			flag = "success";
		} else {
			flag = "fail";
		}
		map.put("flag", flag);
		map.put("billCode", bank.getBillCode());
		return map;

	}

	/**
	 * 付款流程结束，新增放款记录
	 */
	public Map<String, String> saveBankLoan(O2oLoanForm vo) {
		Map<String, String> map = new HashMap<String, String>();
		String flag = "";
		
		O2oBankLoanData bank = new O2oBankLoanData();
		bank.setBillCode(vo.getBillCode());
		bank.setApplyAmount(vo.getApplyAmount());
		bank.setLoanBank(vo.getLoanBankCode());
		bank.setLoanAmount(vo.getLoanAmount());
		bank.setLoanRate(vo.getLoanRate());
		bank.setLoanTerm(vo.getLoanTerm());
		bank.setDataType(vo.getDataType());
		bank.setLoanerName(vo.getLoanerName());
		bank.setLoanerCard(vo.getLoanerCard());
		bank.setPayType(vo.getPayType());
		bank.setIsDel("N");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (vo.getLoanDate() != null) {
			try {
				bank.setLoanDate(sdf.parse(vo.getLoanDate()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		boolean insrtFlag = false;
		// 新增
		Date date = new Date();
		bank.setOprateTime(date);
		User user = UserHolder.getUser();
		if (user != null) {
			bank.setOpratePsn(UserHolder.getUser().getCname());
		}
		try {
			insrtFlag = this.insert(bank) >= 1 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (insrtFlag == true) {
			flag = "success";
		} else {
			flag = "fail";
		}
		map.put("flag", flag);
		map.put("billCode", bank.getBillCode());
		return map;

	}

	/**
	 * 根据billCode查询
	 * 
	 * @param billCode
	 * @return
	 */
	public String isBillCodeExport(String billCode) {
		String key_code = this.findString(
				"select t.bank_loan_id from o2o_bank_loan_data t where t.is_del ='N' and t.bill_code = ?", billCode);
		return key_code == null ? "" : key_code;
	}
	public O2oBankLoanData queryBillCode(String billCode) {
		return this.findEntity("select * from o2o_bank_loan_data t where t.is_del ='N' and t.bill_code = ?", O2oBankLoanData.class, billCode);
	}
}
